package com.example.simple_file_chooser;

/**
 * Created by hp on 2018/8/9.
 */

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;


public class StorageUtil {

    static String packageDir = "";

    static String packageDirImage = "image";

    public static String getPackageDir(Context ctx) {
        if (TextUtils.isEmpty(packageDir)) {
            initPackageDir(ctx);
        }
        return packageDir;
    }

    public static String getPackageDirImage(Context ctx) {
        if (TextUtils.isEmpty(packageDir)) {
            initPackageDir(ctx);
        }
        new File(packageDir + "/" + packageDirImage).mkdirs();
        return packageDir + "/" + packageDirImage;
    }



    public static void initPackageDir(Context ctx) {
        File file = new File(StorageUtil.getPrimaryStoragePath(ctx) + "/" + ctx.getPackageName().replaceAll("\\.", "_"));
        file.mkdirs();
        packageDir = file.getAbsolutePath();

    }

    private static final String TAG = "===StorageUtil===";

    public static String pathsd = "";

    public static String getPrimaryStoragePath(Context context) {

        File[] files;
        List<String> fs;
        if (!TextUtils.isEmpty(pathsd)) {
            return pathsd;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            files = context.getExternalFilesDirs(Environment.MEDIA_MOUNTED);
            fs = new ArrayList<String>(files.length);
            for (File file : files) {
                fs.add(file.getAbsolutePath());
            }
            if (fs.size() == 2) {
                if (!fs.get(0).toLowerCase().contains("emulated")) {
                    String tpath = fs.get(0).toString();
                    int endidx = tpath.indexOf("/", tpath.indexOf("storage") + 8);
                    return pathsd = tpath.substring(0, endidx);
                }
            }
        }
        try {
            StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
            Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths");
            String[] paths = (String[]) getVolumePathsMethod.invoke(sm);
            // first element in paths[] is primary storage path
            return paths[0];
        } catch (Exception e) {
            Log.e(TAG, "getPrimaryStoragePath() failed", e);
        }
        return null;
    }

    public static String getSecondaryStoragePath(Context context) {
        try {
            StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
            Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths");
            String[] paths = (String[]) getVolumePathsMethod.invoke(sm);
            // second element in paths[] is secondary storage path
            return paths[1];
        } catch (Exception e) {
            Log.e(TAG, "getSecondaryStoragePath() failed", e);
        }
        return null;
    }

    public static String getStorageState(Context context, String path) {
        try {
            StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
            Method getVolumeStateMethod = StorageManager.class.getMethod("getVolumeState", new Class[]{String.class});
            String state = (String) getVolumeStateMethod.invoke(sm, path);
            return state;
        } catch (Exception e) {
            Log.e(TAG, "getStorageState() failed", e);
        }
        return null;
    }
}